package com.java.base.exercise.designmode.single;

import java.util.concurrent.TimeUnit;

/**
 * @Author RenPu
 * @Date 2021/6/21 9:48
 * @Version 1.0
 * @Description: 设计模式---单例设计模式V3.0  懒汉式汉式加载  对多线程并发访问的优化
 **/
public class Single_003 {

    private static Single_003 Instance;

    private Single_003() {
    }


    //使用同步锁，避免线程的问题
    public static synchronized Single_003 getInstance() {
        if (Instance == null) {
            try {
                Thread.sleep(TimeUnit.SECONDS.toSeconds(3));
                Instance = new Single_003();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        return Instance;
    }


}
